package com.hex.ds.hdtp.core.app.common.util;

import info.monitorenter.cpdetector.io.ASCIIDetector;
import info.monitorenter.cpdetector.io.CodepageDetectorProxy;
import info.monitorenter.cpdetector.io.JChardetFacade;
import info.monitorenter.cpdetector.io.UnicodeDetector;

import java.io.File;
import java.nio.charset.Charset;

/**
 * @Package: com.hex.ds.hdtp.core.app.common.util
 * @ClassName FileEncodeUtil
 * @Description: 获取文件编码格式
 * @Author: wz.li
 * @Date 2023/5/25 16:01
 * @Version v2.0.2
 **/
public class FileEncodeUtil {

    public static Charset getFileEncode(File file){
        try {
            CodepageDetectorProxy detectorProxy = CodepageDetectorProxy.getInstance();
            detectorProxy.add(ASCIIDetector.getInstance());
            detectorProxy.add(UnicodeDetector.getInstance());
            detectorProxy.add(JChardetFacade.getInstance());
            return detectorProxy.detectCodepage(file.toURI().toURL());
        }catch (Exception e){
            return Charset.forName("UTF-8");
        }
    }

    public static Charset getFileEncode(String filePath){
        return getFileEncode(new File(filePath));
    }

}
